package com.pig4cloud.pig.admin.task;

import com.pig4cloud.pig.admin.api.entity.NewsList;
import com.pig4cloud.pig.admin.constant.CommonConstant;
import com.pig4cloud.pig.admin.task.base.BaseTask;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class BasketBallNews extends BaseTask {

	private final String url = "https://bbs.hupu.com/all-nba";

	@Scheduled(cron = "0 0/5 * * * ?")
	public void doTask() {
		String html = httpUtilService.getRequest(1,this.getClass(),url, CommonConstant.UTF8);
		Document doc = Jsoup.parse(html);
		Elements htmlContent = doc.body().select(".list-wrap");
		for (Element htm : htmlContent){
			String cate = htm.select(".t-label").select("a").text();
			if("篮球资讯".equals(cate) || "湿乎乎的话题".equals(cate) || "CBA专区".equals(cate)){
				NewsList news = NewsList.create(idGenerator.nextId());
				String text = htm.select(".t-title").text();
				String href = htm.select("a").attr("href");
				news.setCategory("篮球热点");
				news.setUrl("https://bbs.hupu.com"+href);
				String topic = (cate.equals("CBA专区")?"【CBA】":"【NBA】")+text;
				news.setTitle(topic);
				news.setCreateTime(CommonConstant.getDay());
				pushNewsToRedis(news);
			}
		}
	}
}
